package com.thread.threadpool;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.thread.model.entity.UserDemo;
import com.thread.model.entity.test1.UserAge;
import com.thread.model.entity.test1.UserName;
import com.thread.model.service.IUserAgeService;
import com.thread.model.service.IUserNameService;

import java.time.LocalDateTime;
import java.util.List;



/**
 * runnable 实现多线程
 * 1. 自定义线程池
 * 2. 构造方法将使用的注入
 * 3. 多线程调用方法进行操作
 * 4. 通过线程池调用runnable
 * @date 21-4-6 21:20
 * @author ccubee
 */
@DS("test1")
public class UserMove implements Runnable {


    private IUserNameService userNameService;
    private IUserAgeService userAgeService;
    private List<UserDemo> list;


    public UserMove(IUserNameService userNameService, IUserAgeService userAgeService, List<UserDemo> list) {
        this.userNameService = userNameService;
        this.userAgeService = userAgeService;
        this.list = list;
    }

    @Override
    @DS("test1")
    public void run() {
       list.forEach(user -> {
            insertUserAge(user);
            insertUserName(user);
       });
    }

    @DS("test1")
    private void insertUserAge(UserDemo userDemo) {
        LocalDateTime now = LocalDateTime.now();
        UserAge userAge = UserAge.builder().userId(userDemo.getId())
                .userAge(userDemo.getAge())
                .userBirth(now)
                .createTime(now)
                .updateTime(now)
                .build();
        userAgeService.save(userAge);
    }

    @DS("test1")
    private void insertUserName(UserDemo userDemo) {
        LocalDateTime now = LocalDateTime.now();
        UserName userName = UserName.builder()
                .userId(userDemo.getId())
                .userName(userDemo.getName())
                .createTime(now)
                .updateTime(now)
                .userPassword(userDemo.getPassword())
                .build();
        userNameService.save(userName);
    }
}